package com.explorati.LeetCode091.numDecodings;

/**
 * @ Author : Weijian_Wang
 * @ Date : Created in 22:55 2020/10/22 0022
 * @ Description ：正向递归
 */
public class Solution2 {

    public int numDecodings(String s) {
        return getAns(s, s.length());
    }

    private int getAns(String s, int end) {
        if (end == 0) {
            return 1;
        }
        if (end == 1) {
            return s.charAt(end-1) == '0' ? 0 : 1;
        }

        int ans1 = 0;
        if (s.charAt(end-1) != '0') {
            ans1 = getAns(s, end - 1);
        }
        int ans2 = 0;

        if (end > 1 && s.charAt(end - 2) != '0') {
            int ten = (s.charAt(end - 2) - '0') * 10;
            int one = s.charAt(end - 1) - '0';
            if (ten + one <= 26) {
                ans2 = getAns(s, end - 2);
            }
        }
        return ans1 + ans2;
    }
}
